#include "formhistroy.h"
#include "ui_formhistroy.h"

FormHistroy::FormHistroy(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormHistroy) {
    ui->setupUi(this);

    ui->dateBegin->setDate(QDate::currentDate().addMonths(-3));
    ui->dateEnd->setDate(QDate::currentDate().addDays(1));

    initAlarmView();

    connect(this, &FormHistroy::alarmPageChanged, [ = ]() {
        ui->AlarmPageFrist->setEnabled(true);
        ui->AlarmPagePrevious->setEnabled(true);
        ui->AlarmPageNext->setEnabled(true);
        ui->AlarmPageEnd->setEnabled(true);

        if(alarmPage == 1) {
            ui->AlarmPageFrist->setEnabled(false);
            ui->AlarmPagePrevious->setEnabled(false);
        }

        if(alarmPage >= alarmPages) {
            ui->AlarmPageNext->setEnabled(false);
            ui->AlarmPageEnd->setEnabled(false);
        }
    });
}

FormHistroy::~FormHistroy() {
    delete ui;
}

void FormHistroy::initAlarmView() {
//    tablemodel = new QSqlTableModel(this);
//    tablemodel->setTable("alarm");
//    tablemodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
//    tablemodel->select();
//    tablemodel->setHeaderData(0, Qt::Horizontal, tr("序号"));
//    tablemodel->setHeaderData(1, Qt::Horizontal, tr("时间"));
//    tablemodel->setHeaderData(2, Qt::Horizontal, tr("控制器地址"));
//    tablemodel->setHeaderData(3, Qt::Horizontal, tr("探测器地址"));
//    tablemodel->setHeaderData(4, Qt::Horizontal, tr("探测器描述"));
//    tablemodel->setHeaderData(5, Qt::Horizontal, tr("报警事件内容"));

//    ui->tableViewAlarm->setModel(tablemodel);
//    ui->tableViewAlarm->setEditTriggers(QAbstractItemView::NoEditTriggers);
//    ui->tableViewAlarm->setColumnWidth(0, 40);
//    ui->tableViewAlarm->setColumnWidth(1, 140);
//    ui->tableViewAlarm->setColumnWidth(2, 80);
//    ui->tableViewAlarm->setColumnWidth(3, 80);
//    ui->tableViewAlarm->setColumnWidth(4, 110);
//    ui->tableViewAlarm->horizontalHeader()->setSectionResizeMode(5, QHeaderView::Stretch);

    alarmmodel = new QSqlQueryModel(this);
    alarmmodel->setQuery("select id,numtime,controller_addr,register_addr,desp,memo from alarm order by numtime desc");
    alarmmodel->setHeaderData(0, Qt::Horizontal, tr("序号"));
    alarmmodel->setHeaderData(1, Qt::Horizontal, tr("时间"));
    alarmmodel->setHeaderData(2, Qt::Horizontal, tr("控制器地址"));
    alarmmodel->setHeaderData(3, Qt::Horizontal, tr("探测器地址"));
    alarmmodel->setHeaderData(4, Qt::Horizontal, tr("探测器描述"));
    alarmmodel->setHeaderData(5, Qt::Horizontal, tr("报警事件内容"));
    QSortFilterProxyModel* sqlproxyA = new QSortFilterProxyModel(this);
    sqlproxyA->setSourceModel(alarmmodel);
    ui->tableViewAlarm->setModel(sqlproxyA);
    ui->tableViewAlarm->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableViewAlarm->setColumnWidth(0, 60);
    ui->tableViewAlarm->setColumnWidth(1, 140);
    ui->tableViewAlarm->setColumnWidth(2, 80);
    ui->tableViewAlarm->setColumnWidth(3, 80);
    ui->tableViewAlarm->setColumnWidth(4, 110);
    ui->tableViewAlarm->horizontalHeader()->setSectionResizeMode(5, QHeaderView::Stretch);
    ui->tableViewAlarm->setAlternatingRowColors(true);
    ui->tableViewAlarm->verticalHeader()->setDefaultSectionSize(20);
    ui->tableViewAlarm->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    MyDelegate *myDele = new MyDelegate;
    ui->tableViewAlarm->setItemDelegate(myDele);
    calcPageSize();
}

void FormHistroy::calcPageSize() {
    QString where_str = QString("where (numtime between '%1' and '%2')").arg(ui->dateBegin->date().toString("yyyy-MM-dd")).arg(ui->dateEnd->date().toString("yyyy-MM-dd"));
    if(ui->comboBoxRegister->currentIndex()>0)
        where_str += QString(" and (register_addr=%1)").arg(ui->comboBoxRegister->currentIndex());
    if(!ui->lineEditMemo->text().isEmpty())
        where_str += QString(" and (memo like '%%1%')").arg(ui->lineEditMemo->text());

    QString sqlcmd = QString("select count(id) from alarm ") + where_str;
//    qDebug() << sqlcmd;
    QSqlQuery query;
    query.exec(sqlcmd);
    query.next();
    recs = query.value(0).toInt();  // 总记录数

    // 计算总页数
    if(recs % alarmSize != 0)
        alarmPages = recs / alarmSize + 1;
    else
        alarmPages = recs / alarmSize;

    alarmPage = 1;
    showAlarm();
    emit alarmPageChanged();
}

void FormHistroy::on_btnClose_2_clicked() {
    close();
}

void FormHistroy::showAlarm() {
    QString sqlcmd;
    QString ppp = tr("第 %1/%2 页。共%3条记录，每页显示").arg(alarmPage).arg(alarmPages).arg(recs);
    ui->alarmPage->setText(ppp);

    QString where_str = QString("where (numtime between '%1' and '%2')").arg(ui->dateBegin->date().toString("yyyy-MM-dd")).arg(ui->dateEnd->date().toString("yyyy-MM-dd"));
    if(ui->comboBoxRegister->currentIndex()>0)
        where_str += QString(" and (register_addr=%1)").arg(ui->comboBoxRegister->currentIndex());
    if(!ui->lineEditMemo->text().isEmpty())
        where_str += QString(" and (memo like '%%1%')").arg(ui->lineEditMemo->text());

    QString limit_str = QString(" order by numtime desc limit %1, %2 ").arg(alarmSize * (alarmPage - 1)).arg(alarmSize);

    sqlcmd = QString("select id,numtime,controller_addr,register_addr,desp,memo from alarm ");

    sqlcmd = sqlcmd + where_str + limit_str;
//    qDebug() << sqlcmd;
    alarmmodel->setQuery(sqlcmd);

}

void FormHistroy::on_AlarmPageFrist_clicked() {
    if(alarmPage > 1) {
        alarmPage = 1;
        showAlarm();
    }
    emit alarmPageChanged();
}


void FormHistroy::on_AlarmPagePrevious_clicked() {
    if(alarmPage > 1) {
        alarmPage--;
        showAlarm();
    }
    emit alarmPageChanged();
}


void FormHistroy::on_AlarmPageNext_clicked() {
    if(alarmPage < alarmPages) {
        alarmPage++;
        showAlarm();
    }
    emit alarmPageChanged();
}


void FormHistroy::on_AlarmPageEnd_clicked() {
    if(alarmPage < alarmPages) {
        alarmPage = alarmPages;
        showAlarm();
    }
    emit alarmPageChanged();
}


void FormHistroy::on_cBoxAlarm_currentIndexChanged(int index) {
    alarmSize = (ui->cBoxAlarm->itemText(index)).toInt();
    calcPageSize();
    qDebug() << alarmSize;
}


void FormHistroy::on_AlarmRefresh_clicked()
{
    calcPageSize();
}



void FormHistroy::on_pushButton_clicked()
{
    calcPageSize();
}

